@isTest
private class QuoteRegentControllerTest {
    static testMethod void testMethod1() {
        // 客户
        Account acc = TestUtils.createAccount();
        acc.Level__c = '一级医院';
        insert acc;

             // 客户
        Account acc1 = TestUtils.createAccount();
        acc1.Name = 'xxxxxxxx';
        acc1.Level__c = '一级医院';

        acc1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Maccura', 'Account');
        insert acc1;
        // 业务机会
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        opp.ExecutiveCompany__c = acc1.Id;
        insert opp;
        Id rt = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        // 产品
        Product__c p = TestUtils.createProduct();
        p.Purpose__c = '仪器报价用';
        p.bggsjhdj__c = 10.0;
        p.Brand__c = 'maccura';
        p.RecordTypeId = rt;

        Product__c p2 = TestUtils.createProduct();
        p2.Name = 'aaaa';
        p2.Purpose__c = '仪器报价用';
        p2.bggsjhdj__c = 10.0;
        p2.RecordTypeId = rt;
        insert new Product__c[] {p, p2};

        // 意向产品
        IntentProduct__c ip = TestUtils.createIntentProduct();
        ip.Opportunity__c = opp.Id;
        ip.Product__c = p.Id;
        insert ip;

        // 价格
        Price__c price = TestUtils.createPrice();
        price.Product__c = p.Id;
        price.Company__c = '四川省迈克实业有限公司';
        price.InstCost__c = 10.0;
        Price__c price2 = TestUtils.createPrice();
        price2.Product__c = p2.Id;
        price2.Company__c = '四川省迈克实业有限公司';
        price2.InstCost__c = 10.0;
        insert new Price__c[] {price, price2};
        
            /// 日期处理
        Integer m = Date.today().month(); //当前月份
        Integer y = Date.today().year();  //当前年份 
       ///取上月
         if(m == 1){
        m = 12 ;
            y -= 1;
        }else{
        m -= 1;
            
        }
        
        String  month = String.valueOf(m) + '月';
        String  year= String.valueOf(y) + '年';
        AveragePrice__c av1 = new AveragePrice__c(
        Product__c = p.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month,
            PriceProvince__c= '四川省/西藏/重庆市'

            
        );
          AveragePrice__c av2 = new AveragePrice__c(
            Product__c = p2.Id,
            Company__c = '四川省迈克实业有限公司',
            AveragePrice__c = 1.0,
            Year__c = year,
            Month__c = month,
            PriceProvince__c= '四川省/西藏/重庆市'

        );
        insert new AveragePrice__c[] {av2,av1};

                // ---------------------新建-----------------------
        PageReference page = new PageReference('/apex/QuoteRegent?oppid=' + opp.Id );
        System.Test.setCurrentPage(page);
        QuoteRegentController qr = new QuoteRegentController(new ApexPages.StandardController(new Quote()));

        qr.init();
        qr.oppId= opp.Id;
        qr.init();
        qr.getDetails();
        qr.getProductFromIntent(opp.Id);
        qr.getLastQuotePrice(opp.Id);
        qr.searchJson =EncodingUtil.urlEncode('{"type":"FromProduct","brand":"","spec":"","cate0":"","cate1":"","nccode":"","ptype":"","serialNo":""}', 'UTF-8') ;
       
        qr.search();

        qr.searchJson =EncodingUtil.urlEncode('{"type":"PriceProduct","brand":"","spec":"","cate0":"","cate1":"","nccode":"","ptype":"","serialNo":""}', 'UTF-8') ;
        qr.search();

        

        List<QuoteRegentController.QuoteProductBean> resList = new List<QuoteRegentController.QuoteProductBean>(); 

        QuoteRegentController.QuoteProductBean ajst = new QuoteRegentController.QuoteProductBean(p2,price2,av2,new PriceList__c() ,null,false);
        ajst.adprice =9.0;
        resList.add(ajst);
        qr.resJson = EncodingUtil.urlEncode(JSON.serialize(resList), 'UTF-8');

        qr.save();
        List<Quote> q = [select Id,Status from Quote where OpportunityId = :opp.Id];
        System.assertEquals(1, q.size());


          // ---------------------编辑-----------------------
        page = new PageReference('/apex/QuQuoteRegentote?id=' + q[0].Id);
        System.Test.setCurrentPage(page);
        qr = new QuoteRegentController(new ApexPages.StandardController(q[0]));

        qr.init();

        qr.getDetails();


    }
}